.org 0x0
.set noat
.set noreorder
.set nomacro
.global _start

_start:
	ori $1,$0,0x100 #跳到主函数执行
	jr $1
	nop
#8个大拨码开关 地址：275  
#8个小拨码开关 地址：274
.org 0x20 #定时器中断入口地址
	ori $10,$0,0x300
	jr $10
	nop
.org 0x30 #设计小时中断入口地址
	ori $10,$0,0x400
	jr $10
	nop
.org 0x40 #设计分钟中断入口地址
	ori $10,$0,0x500
	jr $10
	nop
.org 0x50 #设计秒中断入口地址
	ori $10,$0,0x600
	jr $10
	nop	
	
.org 0x100 #主程序区
	#初始化数码管段表
	lui $1,0x3f06
	ori $1,$1,0x5b4f
	sw $1,0($0)
	
	lui $1,0x666d
	ori $1,$1,0x7d07
	sw $1,4($0)
	
	lui $1,0x7f6f
	ori $1,$1,0x777c
	sw $1,8($0)
	
	#初始化数码管显示时间为12:00:00 $4,$5,$6,$7,$8,$9（hh:mm:ss）
	ori $4,$0,0x1
	ori $5,$0,0x2
	ori $6,$0,0x0
	ori $7,$0,0x0
	ori $8,$0,0x0
	ori $9,$0,0x0
#	movz $6,$0,$0
	#movz $7,$0,$0
	#movz $8,$0,$0
	#movz $9,$0,$0
	
	#初始化compare寄存器为 0x2FAF080s(50000000)
	lui $1,0x2FA
	ori $1,$1,0xF080
	mtc0 $1,$11,0x0
	
	#设置status寄存器为0x10003c01,使能时钟中断,使能三个按键中断
	lui $1,0x1000
	ori $1,$1,0x3c01
	mtc0 $1,$12,0x0
   
	#设置count寄存器为0
	mtc0 $0,$9,0x0
	
_loop:
	#高位数码管共用输入 地址：259
	#低位数码管共用输入 地址：258
	#8个数码管的选择信号 地址：257
	ori $1,$0,0x11
	sb $1,257($0)
	#数据显示到数码管1
	lbu $1,0($9)
	sb $1,258($0)
	#数据显示到数码管5
	lbu $1,0($6)
	sb $1,259($0)
	jal delay
	nop
	
	ori $1,$0,0x22
	sb $1,257($0)
	#数据显示到数码管2
	lbu $1,0($8)
	sb $1,258($0)
	#数据显示到数码管6
	ori $1,$0,0x40
	sb $1,259($0)
	jal delay
	nop
	
	ori $1,$0,0x44
	sb $1,257($0)
	#数据显示到数码管3
	ori $1,$0,0x40
	sb $1,258($0)
	#数据显示到数码管7
	lbu $1,0($5)
	sb $1,259($0)
	jal delay
	nop
	
	ori $1,$0,0x88
	sb $1,257($0)
	#数据显示到数码管4
	lbu $1,0($7)
	sb $1,258($0)
	#数据显示到数码管8
	lbu $1,0($4)
	sb $1,259($0)
	jal delay
	nop
	
	j _loop
	nop

delay:
	ori $1,$0,0xfff
	ori $2,$0,0x1
_d:	
	sub $1,$1,$2
	bne $1,$0,_d
	nop
	jr $31
	nop
	
	
.org 0x300 #定时器中断处理
	addi $9,$9,0x1
	ori $10,$0,10
	bne $9,$10,timer 
	nop
	#秒的个位产生进位，处理进位
	ori $9,$0,0x0
	addi $8,$8,0x1
	ori $10,$0,6
	bne $8,$10,timer 
	nop
	#秒的十位产生进位，处理进位
	ori $8,$0,0
	addi $7,$7,0x1
	ori $10,$0,10
	bne $7,$10,timer 
	nop
	#分的个位产生进位，处理进位
	ori $7,$0,0
	addi $6,$6,0x1
	ori $10,$0,6
	bne $6,$10,timer 
	nop
	#分的十位产生进位，处理进位
	ori $6,$0,0
	addi $5,$5,0x1
	ori $10,$0,0x2
	bne $4,$10,t
	nop
	ori $10,$0,0x4
	bne $5,$10,timer
	#时的十位为2时，的十位产生进位，处理进位
	ori $5,$0,0
	addi $4,$4,0x1
	ori $10,$0,0x3
	bne $4,$10,timer
	#时的十位产生进位，处理进位
	ori $4,$0,0x0
	j timer
	nop
t:
	ori $10,$0,10
	bne $5,$10,timer 
	nop
	#时的十位为0,1时，时的个位产生进位，处理进位
	ori $5,$0,0
	addi $4,$4,0x1
	ori $10,$0,0x3
	bne $4,$10,timer
	#时的十位产生进位，处理进位
	ori $4,$4,0x0
	
timer:
	#设置compare寄存器为 0x2FAF080(50000000)
	lui $10,0x2FA
	ori $10,$10,0xF080
	mtc0 $10,$11,0x0
	
	#设置count寄存器为0
	mtc0 $0,$9,0x0
	
	eret
	nop
	
.org 0x400 #小时中断处理
	lb $20,275($0)
	andi $21,$20,0x000f #找到个位
	srl $20,$20,4
	andi $22,$20,0x000f #找到十位
#	ori $22,$0,2
#	bne $4,$22,tt
	#处理十位为2
#	ori $23,$0,4
	#subu $21,$21,$22
	#ori $23,$0,3
	#subu $22,$21,$22
	#bgez $21,r3
	#nop
	#bgez $22,r3
	#nop
	#movz $4,$22,$0
	#movz $5,$21,$0
	#j r3
	#nop
tt:
	#处理十位为1
	##ori $23,$0,10
	#subu $21,$21,$22
	#ori $23,$0,3
	#subu $22,$21,$22
	#bgez $21,r3
	#nop
	#bgez $22,r3
	#nop
	movz $4,$22,$0
	movz $5,$21,$0

r3:
	eret
	nop
.org 0x500 #分钟中断处理
	lb $20,275($0)
	andi $21,$20,0x000f #找到个位
	srl $20,$20,4
	andi $22,$20,0x000f #找到十位
	#ori $23,$0,10
	#subu $21,$21,$22
	#ori $23,$0,6
	#subu $22,$21,$22
#	bgez $21,r2
#	nop
#	bgez $22,r2
#	nop
	movz $6,$22,$0
	movz $7,$21,$0
r2:
	eret
	nop
.org 0x600 #秒中断处理
	lb $20,275($0)
	andi $21,$20,0x000f #找到个位
	srl $20,$20,4
	andi $22,$20,0x000f #找到十位
	#ori $23,$0,10
	#subu $21,$21,$22
	#ori $23,$0,6
	#subu $22,$21,$22
	#bgez $21,r1
	#nop
	#bgez $22,r1
	#nop
	movz $8,$22,$0
	movz $9,$21,$0
	
r1:
	eret
	nop